﻿/**************************************************************
 *
 * 唯一标识：36bb593a-fc93-4a45-93f4-7c41318bd4bd
 * 命名空间：Sgr.UPMS.Application.Commands.Users
 * 创建时间：2023/8/27 11:34:55
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：创建用户命令处理器
 *
 **************************************************************/

using MediatR;
using Sgr.Application.Commands;
using Sgr.Application.Queries;
using Sgr.Exceptions;
using Sgr.Identity.Services;
using Sgr.UPMS.Domain.Departments;
using Sgr.UPMS.Domain.Duties;
using Sgr.UPMS.Domain.Users;
using System.Threading;
using System.Threading.Tasks;

namespace Sgr.UPMS.Application.Commands.Users
{
    /// <summary>
    /// 创建用户命令处理器
    /// </summary>
    public class CreateUserCommandHandle : IRequestHandler<CreateUserCommand, CommandResult<bool>>
    {
        private readonly IUserManage _userManage;
        private readonly IUserRepository _userRepository;
        private readonly ICurrentUser _currentUser;
        private readonly IPasswordHashService _passwordHashService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userManage">用户管理器</param>
        /// <param name="userRepository">用户仓储</param>
        /// <param name="currentUser"></param>
        /// <param name="passwordHashService">密码哈希服务</param>
        public CreateUserCommandHandle(IUserManage userManage,
            IUserRepository userRepository,
            ICurrentUser currentUser,
            IPasswordHashService passwordHashService)
        {
            _userManage = userManage;
            _userRepository = userRepository;
            _currentUser = currentUser;
            _passwordHashService = passwordHashService;
        }

        /// <summary>
        /// 处理创建用户命令
        /// </summary>
        /// <param name="request">命令请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处理结果</returns>
        public async Task<CommandResult<bool>> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            long orgId = QueryHelper.GetOrgId(_currentUser);

            if (request.OrgId.HasValue && request.OrgId.Value > 0)
                orgId = request.OrgId.Value;

            // 创建用户
            User user = await _userManage.CreateNewAsync(
                request.LoginName,
                _passwordHashService,
                request.LoginPassword,
                false,
                orgId,
                cancellationToken);

            // 设置用户信息：名称、部门、岗位
            user.SetUserInfo(request.UserName, request.Gender, null, null);
            user.SetDepartment(request.DepartmentId, "");

            user.SetPrimaryDuty(request.PrimaryDutyId, "");
            if (request.DutyIds != null)
                user.DifferenceUpdateDuties(request.DutyIds!);

            // 绑定所属角色
            if (request.RoleIds != null)
                user.DifferenceUpdateRoles(request.RoleIds!);

            // 保存用户到仓储
            await _userRepository.InsertAsync(user, cancellationToken);

            // 返回成功结果
            return CommandResult.Success(true, "用户创建成功");
        }
    }
}